<?php

    class SitemapController extends Controller
    {
        private $urls = 25000;

        function _get_parts($count, $type, $urls){ 
            $num_part = ceil($count / $urls);
            $parts = array();
            for($i = 1; $i <= $num_part; $i++){
                $parts[] = $this->createAbsoluteUrl('/sitemap/part', array('type' => $type, 'part' => sprintf('%03d', $i)));
            }
            return $parts;
        }
        
        
    public function actionRobots(){
        header("Content-type: text/plain");
echo "User-agent: *
Crawl-delay: 10
Sitemap: {$this->baseUrl}/sitemap.xml";
    }

        public function actionIndex()
        {   
            header('Content-Type: text/xml');

            $parts = array(
                'main' => array($this->createAbsoluteUrl('sitemap/part', array('type' => 'main'))),
            );

            $count_item_row = Post::model()->count('`status` = "ENABLE"');
            $parts['item']     = $this->_get_parts($count_item_row, 'item', $this->urls); 
            
            $this->renderPartial('indexs', compact('parts'));
        }
        
        public function actionPart($type = 'main', $part = 1){
            header('Content-Type: text/xml');
            $data = array();  

            if($type == 'item'){
                $part = intval($part);
                if(!$part) die;

                $time = date('Y-m-d');
                $offset = ($part-1) * $this->urls;
                
                $items = Post::model()->findAll(new CDbCriteria(array(
                    'condition' => '`status` = "ENABLE"',
                    'order' => 'id ASC',
                    'limit' => $this->urls,
                    'offset' => $offset,
                )));

                foreach($items as $item){
                      $data[] = array(
                        'loc' => $item->url,   
                        'lastmod' =>  date('Y-m-d', strtotime($item->changed)),
                        'changefreq' => 'daily',
                        'priority'   => '0.5',
                    ); 
                }
            }
            
            elseif($type == 'main'){
                //links
                $links[] = $this->baseUrl;
                $links[] = $this->createAbsoluteUrl('/web/page/price');
                $links[] = $this->createAbsoluteUrl('/web/page/qna');
                $links[] = $this->createAbsoluteUrl('/web/page/contact');
                
                foreach($links as $link){
                    $data[] = array(
                        'loc' => $link,
                        'lastmod' => date('Y-m-d'),
                        'changefreq' => 'weekly',
                        'priority'   => '1.0',
                    );
                }
            }
            
            $this->renderPartial('part', array(
                'items' => $data
            ));

        }
    }
